package com.uit.trip_.tab_activity.childs;

import com.uit.trip_.R;
import com.uit.trip_.data.TripDatabase;
import com.uit.trip_.models.WallAppItem;
import com.uit.trip_.utils.ProcessImage;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class WallItemDetailActivity extends Activity {

	private static final int GALLERY_REQUESTCODE = 100;
	private EditText etContent;
	private EditText etAddress;
	private String mSelectedImagePath = null;
	private ImageView imgContent;
	private TripDatabase db;
	private String date;
	private String imagePath;
	private int id;
	private int trip_id;
	private int position;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.wall_item_detail_activity);
		etContent = (EditText) findViewById(R.id.etContentDetail);
		etAddress = (EditText) findViewById(R.id.etAddressDetail);
		imgContent = (ImageView) findViewById(R.id.imgWallDetail);
		db = new TripDatabase(this);

		Bundle extras = getIntent().getExtras();
		if (extras == null)
			return;
		position = extras.getInt("position");
		id = extras.getInt("id");
		trip_id = extras.getInt("id_trip");
		date = extras.getString("date");
		imagePath = extras.getString("imagePath");
		if (imagePath != null && !imagePath.equalsIgnoreCase("")) {
			Bitmap bm = ProcessImage.getInstance(this).getBitmapFromFile(
					imagePath);
			imgContent.setImageBitmap(bm);
		}
		String content = extras.getString("name");
		String address = extras.getString("address");

		etContent.setText("" + content);
		etAddress.setText("" + address);
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btSaveDetail:
			if (mSelectedImagePath != null
					&& !mSelectedImagePath.equalsIgnoreCase("")) {
				imagePath = mSelectedImagePath;
			}
			WallAppItem item = new WallAppItem(id, trip_id, etContent.getText()
					.toString(), date, etAddress.getText().toString(),
					imagePath);
			db.updateToNote(item);
			Intent intent = new Intent();
			intent.putExtra("position", position);
			setResult(RESULT_OK, intent);
			finish();
			break;

		case R.id.btCancelDetail:
			finish();
			break;
		case R.id.btAddImage:
			Intent i = new Intent(Intent.ACTION_GET_CONTENT);
			i.setType("image/*");
			startActivityForResult(Intent.createChooser(i, "Select image"),
					GALLERY_REQUESTCODE);
			break;
		default:
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
			case GALLERY_REQUESTCODE:
				Uri imgUri = data.getData();
				mSelectedImagePath = getPath(imgUri);
				Bitmap bm = ProcessImage.getInstance(getApplicationContext())
						.getBitmapFromFile(mSelectedImagePath);
				imgContent.setImageBitmap(bm);
				break;
			default:
				break;
			}
		}
	}

	private String getPath(Uri uri) {
		String[] projection = { MediaStore.Images.Media.DATA };
		Cursor cursor = managedQuery(uri, projection, null, null, null);
		int column_index = cursor
				.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
		cursor.moveToFirst();
		return cursor.getString(column_index);
	}
}
